package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mapper.OrderRegisterJzjMapper;
import com.ruoyi.service.JurisdictionService;
import com.ruoyi.service.OrderRegisterJzjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderRegisterJzjServiceImpl implements OrderRegisterJzjService {

    @Autowired
    OrderRegisterJzjMapper orderRegisterJzjMapper;

    @Autowired
    JurisdictionService jurisdictionService;

    @Override
    public List<Map> getHotelJjzList(Map map) {
        map.put("jurisdiction", jurisdictionService.getHotelDataPermissionsAndCompleteSQL());
        return orderRegisterJzjMapper.getHotelJjzList(map);
    }

    @Override
    public Map getHotelJzjByHotelId(String hotelId) {
        return orderRegisterJzjMapper.getHotelJzj(hotelId);
    }

    @Override
    public Map getHotelJjzById(String id) {
        return orderRegisterJzjMapper.getHotelJjzById(id);
    }

    @Override
    public int postEditHotelJzj(Map map) {
        return orderRegisterJzjMapper.postEditHotelJzj(map);
    }

    @Override
    public int postAddHotelJzj(Map map) {
        return orderRegisterJzjMapper.postAddHotelJzj(map);
    }

    /**
     * 出结转金
     *
     * @param hotelId
     * @param price
     * @return
     */
    @Override
    public int outJzj(String hotelId, BigDecimal price) {
        Map hotelJzj = orderRegisterJzjMapper.getHotelJzj(hotelId);
        BigDecimal jzjZcTotal = new BigDecimal(0);
        BigDecimal jzjPrice = new BigDecimal(0);
        if (hotelJzj != null) {
            jzjZcTotal = new BigDecimal(hotelJzj.get("jzjZcTotal").toString());
            jzjPrice = new BigDecimal(hotelJzj.get("jzjPrice").toString());
        }

        jzjZcTotal = jzjZcTotal.add(price);
        jzjPrice = jzjPrice.subtract(price);

        Map map1 = new HashMap();
        map1.put("hotelId", hotelId);
        map1.put("jzjZcTotal", jzjZcTotal);
        map1.put("jzjPrice", jzjPrice);
        return orderRegisterJzjMapper.updateHotelJzj(map1);
    }

    /**
     * 进结转金
     *
     * @param hotelId
     * @param price
     * @return
     */
    @Override
    public int inJzj(String hotelId, BigDecimal price) {
        // 结转金信息
        Map hotelJzj = orderRegisterJzjMapper.getHotelJzj(hotelId);
        BigDecimal jzjZrTotal = new BigDecimal(0);
        BigDecimal jzjPrice = new BigDecimal(0);
        if (hotelJzj != null) {
            jzjZrTotal = new BigDecimal(hotelJzj.get("jzjZrTotal").toString());
            jzjPrice = new BigDecimal(hotelJzj.get("jzjPrice").toString());
        }

        jzjZrTotal = jzjZrTotal.add(price);
        jzjPrice = jzjPrice.add(price);

        Map map1 = new HashMap();
        map1.put("hotelId", hotelId);
        map1.put("jzjZrTotal", jzjZrTotal);
        map1.put("jzjPrice", jzjPrice);

        int i1 = 0;
        if(orderRegisterJzjMapper.getJzjCountByHotelId(hotelId) > 0){
            i1 = orderRegisterJzjMapper.updateHotelJzj(map1);
        } else {
            i1 = orderRegisterJzjMapper.insertHotelJzj(map1);
        }
        return i1;
    }
}
